﻿using System;
using System.Configuration;
using System.Web;
using System.Web.Configuration;

namespace GateKeeper
{
    public class NewConfigFile
    {
        /// <summary>
        /// Builds a new application config file in the ~/app_data folder
        /// </summary>
        /// <param name="configFileName">
        /// Specify the name of the config file to create
        /// ex. myapp.config
        /// </param>
        /// <returns>Configuration object</returns>
        public static Configuration Create(string configFileName)
        {
            GateKeeperModule.log.DebugFormat("Creating new config file : [{0}]", configFileName);

            try
            {
                WebConfigurationFileMap fileMap = new WebConfigurationFileMap();
                string physDir = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);
                GateKeeperModule.log.DebugFormat("physDir : {0}", physDir);

                VirtualDirectoryMapping vDirMap = new VirtualDirectoryMapping(physDir, true, configFileName);
                GateKeeperModule.log.Debug("vDirMap : Mapped");

                fileMap.VirtualDirectories.Add("/", vDirMap);

                // Documented bug that requires OpenMappedWebConfiguration overload to have three items instead of just two
                // and it doesn't matter what string is inserted
                Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(fileMap, "/App_Data", Environment.MachineName);
                GateKeeperModule.log.Debug("config : Created");
                
                config.AppSettings.Settings.Add("enablehoneypotlogging", "True");
                config.AppSettings.Settings.Add("hotlinkallowedsites", "(google\\.|search\\?q=cache)");
                config.AppSettings.Settings.Add("enablegatekeeper", "True");
                config.AppSettings.Settings.Add("displaycontactform", "False");
                config.AppSettings.Settings.Add("contactformurl", "GateKeeper/AccessDenied.aspx");
                config.AppSettings.Settings.Add("enablehoneypotstats", "True");
                config.AppSettings.Settings.Add("smtpenablessl", "False");
                config.AppSettings.Settings.Add("honeypotpath", "honeypot/trap.aspx");
                config.AppSettings.Settings.Add("smtpusername", "");
                config.AppSettings.Settings.Add("honeypotstatspath", "GateKeeper/Stats.aspx");
                config.AppSettings.Settings.Add("notifyadmin", "False");
                config.AppSettings.Settings.Add("denyemptyuseragent", "False");
                config.AppSettings.Settings.Add("smtpserverport", "25");
                config.AppSettings.Settings.Add("virtualpath", "/");
                config.AppSettings.Settings.Add("storepasswordencrypted", "False");
                config.AppSettings.Settings.Add("smtpmessagesubject", "Suspected SPAM-Bot has been harvested");
                config.AppSettings.Settings.Add("hotlinkexpression", "\\.(gif|jpg|png|bmp)$");
                config.AppSettings.Settings.Add("persisthoneypotdeny", "True");
                config.AppSettings.Settings.Add("displayhotlinkdenyimage", "False");
                config.AppSettings.Settings.Add("hotlinkdenyimage", "");
                config.AppSettings.Settings.Add("enablehoneypot", "True");
                config.AppSettings.Settings.Add("blockhotlinking", "True");
                config.AppSettings.Settings.Add("smtppassword", "");
                config.AppSettings.Settings.Add("smtpmessagebody", "");
                config.AppSettings.Settings.Add("smtpemailaddress", "");
                config.AppSettings.Settings.Add("smtpservername", "");
                config.AppSettings.Settings.Add("accessdeniedmessage", "<div style='text-align: center; color: maroon'><h1>Access Denied</h1><h3>Unfortunately, due to your abuse the request has been denied...</h3><p>If you feel this is an error, send an email to the webmaster of this domain,<br /> or if you are an ill-behaving Bot, Crawler, or Spider then just go away.</p><p style='color: navy; padding-top: 20px;'>Have a nice day!</p></div>");
                
                config.AppSettings.Settings.Add("enablehttpbl", "False");
                config.AppSettings.Settings.Add("enablehttpbllogging", "False");
                config.AppSettings.Settings.Add("denyhttpblsuspect", "False");
                config.AppSettings.Settings.Add("httpblpostonly", "False");
                config.AppSettings.Settings.Add("httpblkeycode", "");
                config.AppSettings.Settings.Add("httpbltestipaddress", "");
                config.AppSettings.Settings.Add("httpbltimeout", "3000");
                config.AppSettings.Settings.Add("threatscorethreshold", "254");

                config.AppSettings.Settings.Add("enableproxybl", "False");
                config.AppSettings.Settings.Add("enableproxybllogging", "False");
                config.AppSettings.Settings.Add("denyproxyblsuspect", "False");
                config.AppSettings.Settings.Add("proxyblpostonly", "False");
                config.AppSettings.Settings.Add("proxybltestipaddress", "");
                config.AppSettings.Settings.Add("proxybltimeout", "3000");

                config.AppSettings.Settings.Add("enabledronebl", "False");
                config.AppSettings.Settings.Add("enabledronebllogging", "False");
                config.AppSettings.Settings.Add("denydroneblsuspect", "False");
                config.AppSettings.Settings.Add("droneblpostonly", "False");
                config.AppSettings.Settings.Add("dronebltestipaddress", "");
                config.AppSettings.Settings.Add("dronebltimeout", "3000");

                GateKeeperModule.log.Debug("Saving config file");
                config.Save(ConfigurationSaveMode.Full);
                GateKeeperModule.log.Debug("Config file saved");
                return config;
            }
            catch (Exception ex)
            {
                GateKeeperModule.log.WarnFormat("NewConfigFile Error : {0}", ex.Message);
                GateKeeperModule.log.Error(ex.StackTrace);
                throw ex;
            }
        }
    }
}
